package com.ebupt.common.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer

abstract class BaseFragment<FragmentBinding:ViewDataBinding> :Fragment(){

    protected lateinit var viewDataBinding: FragmentBinding

    @LayoutRes
    abstract fun getLayoutId():Int

   abstract fun bindView(view:View,savedInstanceState:Bundle?):FragmentBinding


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        initConfig()
        return inflater.inflate(getLayoutId(),container,false)
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewDataBinding = bindView(view,savedInstanceState).also {
            it.lifecycleOwner = viewLifecycleOwner
        }

        initData()

    }


    fun initConfig(){

    }


    fun initData(){

    }


    /**
     * LiveData注册监听
     */
    protected inline fun<T:Any> LiveData<T>.observerKt(crossinline blocak:(T?)->Unit){
        this.observe(this@BaseFragment, Observer {
            blocak.invoke(it)
        })
    }






}